﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Smartmanufact
{
    public partial class login : Form
    {
        public MainView mv = null;
        public login()
        {
            InitializeComponent();
        }

        public void Set_Object_Location()
        {
            // 设定位置
            Rectangle rect = System.Windows.Forms.SystemInformation.VirtualScreen;
            label2.Left = (int)(rect.Width * 0.35);
            label2.Top = (int)(rect.Height * 0.35);

            label3.Left =(int)(rect.Width * 0.35);
            label3.Top = (int)(rect.Height * 0.4);

            textBox_username.Left = (int)(rect.Width * 0.42);
            textBox_username.Top = (int)(rect.Height * 0.415-textBox_username.Height/2);
            textBox_username.Width = (int)(rect.Width * 0.235);
            //textBox_username.Height = (int)(rect.Height * 0.1);

            textBox_password.Left = (int)(rect.Width * 0.42);
            textBox_password.Top = (int)(rect.Height * 0.517-textBox_password.Height/2);
            textBox_password.Width = (int)(rect.Width * 0.235);
           

            button1.Left = (int)(rect.Width * 0.35);
            button1.Top = (int)(rect.Height * 0.5);

            button2.Left = (int)(rect.Width * 0.45);
            button2.Top = (int)(rect.Height * 0.6);

            label1.Top = (int)(rect.Height * 0.6);
            label1.Left = (int)(rect.Width * 0.3);

            label4.Top = (int)(rect.Height * 0.6);
            label4.Left = (int)(rect.Width * 0.52);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // 登陆确定按钮
            if (textBox_username.Text.Trim() == "") { MessageBox.Show("用户名不能为空！"); return; }
            if (textBox_password.Text.Trim() == "") { MessageBox.Show("密码不能为空！"); return; }

            if (textBox_username.Text.Trim() == "admin" && textBox_password.Text.Trim() == "admin") 
            {
                // 是管理员登陆
                this.Hide();     // 界面不可见
                MainView.online = true;
                MainView.username = "admin";
                MainView.limit = "admin";   //登陆的权限是管理员权限
                mv.Show_ALL_Button();
                mv.Select_Button(1);
                mv.Show_Init_View_Once();
                MessageBox.Show("管理员登陆成功！");
                
                return;
            } 

            // 读取所有的用户信息
            try
            {
                DataSet ds = MainView.mysql.sql_search_database("select * from usertable");
                int rowcount = ds.Tables[0].Rows.Count;
                for(int i=0;i<rowcount;i++)
                {
                    DataRow dr = ds.Tables[0].Rows[i];
                    if(dr[0].ToString().Trim()==textBox_username.Text.Trim() && dr[1].ToString().Trim()==textBox_password.Text.Trim())
                    {
                        // 用户名写密码相同的话，那么就可以进入系统
                        this.Hide();
                        MainView.online = true;
                        MainView.username = dr[0].ToString().Trim();
                        MainView.limit = dr[2].ToString().Trim();
                        mv.Show_ALL_Button();
                        MessageBox.Show("用户登陆成功！");
                        mv.Select_Button(1);
                        return;
                    }
                }
            }
            catch { MessageBox.Show("登陆系统出现错误！"); }
            MessageBox.Show("用户名或密码错误！");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void login_Load(object sender, EventArgs e)
        {
            Set_Object_Location();
        }
        public void Clear_PassWord()
        {
            
            textBox_password.Text = "";
        }

        private void label1_Click(object sender, EventArgs e)
        {
            if (textBox_username.Text.Trim() == "") { MessageBox.Show("用户名不能为空！"); return; }
            if (textBox_password.Text.Trim() == "") { MessageBox.Show("密码不能为空！"); return; }

            if (textBox_username.Text.Trim() == "admin" && textBox_password.Text.Trim() == "admin")
            {
                // 是管理员登陆
                this.Hide();     // 界面不可见
                MainView.online = true;
                MainView.username = "admin";
                MainView.limit = "admin";   //登陆的权限是管理员权限
                mv.Show_ALL_Button();
                mv.Select_Button(1);
                mv.Show_Init_View_Once();
                mv.Set_Panel();
                MessageBox.Show("管理员登陆成功！");
                return;
            }

            // 读取所有的用户信息
            try
            {
                DataSet ds = MainView.mysql.sql_search_database("select * from usertable");
                int rowcount = ds.Tables[0].Rows.Count;
                for (int i = 0; i < rowcount; i++)
                {
                    DataRow dr = ds.Tables[0].Rows[i];
                    if (dr[0].ToString().Trim() == textBox_username.Text.Trim() && dr[1].ToString().Trim() == textBox_password.Text.Trim())
                    {
                        // 用户名写密码相同的话，那么就可以进入系统
                        this.Hide();
                        MainView.online = true;
                        MainView.username = dr[0].ToString().Trim();
                        MainView.limit = dr[2].ToString().Trim();
                        mv.Show_ALL_Button();
                        mv.Set_Panel();
                        MessageBox.Show("用户登陆成功！");
                        mv.Select_Button(1);
                        return;
                    }
                }
            }
            catch { MessageBox.Show("登陆系统出现错误！"); }
            MessageBox.Show("用户名或密码错误！");
        }

        private void label4_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
    }
}
